import { useEffect, useState } from 'react';
import { Switch, Modal, Form, Input } from 'antd';
import { createdManufacturer } from '../../../api/manufacturer';


interface ModalProps {
    visible: boolean;
    handleCancel: () => void
    callback: () => void
}

const ManufacturerModal = (props: ModalProps) => {
    const { visible, handleCancel, callback } = props
    const [form] = Form.useForm();
    // 获取统计数据
    // useEffect(() => {
    //     const fetchData = async () => {
    //         try {
    //             setLoading(true);
    //             // 并行请求数据
    //             const [statsData, ManufacturerModalsData] = await Promise.all([
    //                 getManufacturerModalStats(),
    //                 getRecentManufacturerModals()
    //             ]);
    //             setStats(statsData.data);
    //             setRecentManufacturerModals(ManufacturerModalsData.data);
    //         } catch (error) {
    //             console.error('获取首页数据失败', error);
    //         } finally {
    //             setLoading(false);
    //         }
    //     };

    //     fetchData();
    // }, []);
    const handleOk = () => {
        form
            .validateFields()
            .then(async (res) => {
                const next = { ...res, status: Number(res.status) }
                await createdManufacturer(next)
                callback()
            })
            .catch((err) => console.log('新建厂商发生错误:', err));
    }


    return (
        <Modal
            title="新建厂商"
            closable={{ 'aria-label': 'Custom Close Button' }}
            open={visible}
            onOk={handleOk}
            onCancel={handleCancel}
            width={800}
        >
            <div className='mt-10'>
                <Form
                    name="basic"
                    layout='horizontal'
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    style={{ maxWidth: 600 }}
                    initialValues={{ remember: true }}
                    autoComplete="off"
                    form={form}
                >
                    <Form.Item
                        label="厂商名称"
                        name="name"
                        rules={[{ required: true, message: '请输入厂商名称!' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        label="负责人"
                        name="contact_person"
                        rules={[{ required: true, message: '请输入负责人名字!' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="联系电话"
                        name="contact_phone"
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item name="status" label='状态' valuePropName="checked">
                        <Switch checkedChildren="启用" unCheckedChildren="禁用" defaultChecked />
                    </Form.Item>
                </Form>
            </div>
        </Modal>
    )
};

export default ManufacturerModal;
